package com.example.wm.SanWar.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.wm.SanWar.VO.MembershiprosterVo;
import com.example.wm.SanWar.entity.AlliesandUser;
import com.example.wm.SanWar.entity.Membershiproster;
import com.example.wm.SanWar.entity.NoteEntity;
import com.example.wm.SanWar.entity.RosterMemberList;
import com.example.wm.SanWar.mapper.MembershiprosterMapper;
import com.example.wm.SanWar.mapper.RosterMemberListMapper;
import com.example.wm.SanWar.service.AlliesandUserService;
import com.example.wm.SanWar.service.MembershiprosterService;
import com.example.wm.SanWar.service.RosterMemberListService;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.utils.TsyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/MembershiprosterController")
public class MembershiprosterController {
    @Resource
    private MembershiprosterService membershiprosterService;
    @Resource
    private MembershiprosterMapper membershiprosterMapper;
    @Resource
    private TsyUtils tsyUtils;
    @Resource
    private RosterMemberListService rosterMemberListService;
    @Resource
    private RosterMemberListMapper rosterMemberListMapper;
    @Resource
    private AlliesandUserService alliesandUserService;
    @Transactional
    @PostMapping("/Insert")
    public Result Insert(@RequestBody Membershiproster membershiproster) {
        membershiproster.setCreatetime(tsyUtils.getnowtime());
       Boolean b= membershiprosterService.save(membershiproster);
        if(b)
        {
            return ResultData.success(membershiproster.getId());

        }else{
            return ResultData.error(202, String.valueOf(b));
        }

    }
    @Transactional
    @PostMapping("/update")
    public Result update(@RequestBody Membershiproster membershiproster) {
        membershiproster.setCreatetime(tsyUtils.getnowtime());
        Boolean b= membershiprosterService.updateById(membershiproster);
        if(b)
        {
            return ResultData.success(membershiproster.getId());

        }else{
            return ResultData.error(202, String.valueOf(b));
        }

    }
    @Transactional
    @PostMapping("/deleteallbyid")
    public Result deleteallbyid(@RequestBody Membershiproster membershiproster) {
        LambdaQueryWrapper<RosterMemberList> lq=new LambdaQueryWrapper<>();
        lq.eq(RosterMemberList::getMcid,membershiproster.getId());

        List<RosterMemberList> lists=rosterMemberListService.list(lq);

        if(lists.size()>0){
          rosterMemberListService.removeBatchByIds(lists);
        }

        membershiprosterService.removeById(membershiproster);

            return ResultData.success("删除成功");



    }
    @Transactional
    @PostMapping("/findbycreatename")
    public Result findbycreatename(@RequestBody Membershiproster membershiproster) {
        LambdaQueryWrapper<Membershiproster> lq=new LambdaQueryWrapper<>();
        lq.eq(Membershiproster::getCreatenameid,membershiproster.getCreatenameid());
        List<Membershiproster> list=membershiprosterService.list(lq);

        if(!list.isEmpty()){


            return ResultData.success(list);

        }else{
            return ResultData.error(202, "无数据");
        }

    }
    @Transactional
    @PostMapping("/findbycreatename2")
    public Result findbycreatename2(@RequestBody List<String> membershiprosterList) {
        List<RosterMemberList> listroster=new ArrayList<>();
            for(String ms:membershiprosterList){
                LambdaQueryWrapper<RosterMemberList> roslq=new LambdaQueryWrapper<>();
                roslq.eq(RosterMemberList::getMcid, Integer.valueOf(ms) );
                List<RosterMemberList> lists=   rosterMemberListMapper.selectList(roslq);
                if(!lists.isEmpty()){
                    listroster.addAll(lists);
                }
            }

            return ResultData.success(listroster);



    }
    @Transactional
    @PostMapping("/findall")
    public Result findall(@RequestBody JSONObject jsonObject) {
          LambdaQueryWrapper<Membershiproster> lq=new LambdaQueryWrapper<>();
          lq.eq(Membershiproster::getCreatenameid,jsonObject.getString("createnameid"));
          List<Membershiproster> list=membershiprosterService.list(lq);
          List<MembershiprosterVo> membershiprosterVoList=new ArrayList<>();


          if(!list.isEmpty()){
              for(Membershiproster mb:list){
                  // 获取操作人名字

                  mb.setCreatename(alliesandUserService.getById(mb.getCreatenameid()).getName());

                  // 获取成员信息
                  MembershiprosterVo membershiprosterVo=new MembershiprosterVo();
                  LambdaQueryWrapper<RosterMemberList> rlq=new LambdaQueryWrapper<>();
                  rlq.eq(RosterMemberList::getMcid,mb.getId());
                  List<RosterMemberList> rlist=rosterMemberListMapper.selectList(rlq);
                  if(!rlist.isEmpty()){
                      rlist.forEach(r->{
                        LambdaQueryWrapper<AlliesandUser> lqa=new LambdaQueryWrapper<>();
                        lqa.eq(AlliesandUser::getId,r.getNameid());
                        AlliesandUser one = alliesandUserService.getOne(lqa);
                        if(!Objects.equals(one.getName(), r.getName())) {
                            r.setName(one.getName());
                            rosterMemberListMapper.updateById(r);

                        }
                          LambdaQueryWrapper<AlliesandUser> lqa2=new LambdaQueryWrapper<>();
                          lqa2.eq(AlliesandUser::getId,r.getOpernameid());
                          AlliesandUser one2 = alliesandUserService.getOne(lqa2);
                          if(!Objects.equals(one2.getName(), r.getOpername())) {
                              r.setOpername(one2.getName());

                              rosterMemberListMapper.updateById(r);

                          }
                      });
                  }

                  membershiprosterVo.setMembershiproster(mb);
//                  if(!rlist.isEmpty()){
                      membershiprosterVo.setRosterMemberLists(rlist);
//                  }
                  membershiprosterVoList.add(membershiprosterVo);

              }
              return ResultData.success(membershiprosterVoList);

          }else{
              return ResultData.error(202,"无数据");

          }

    }



}
